//! \file semaphore.hpp
//! \brief Purewell Library Semaphore Header
//! \author Yubin Lim (purewell@gmail.com)
//! \since 2008-01-25

#ifndef __SEMAPHORE_HPP__
#define __SEMAPHORE_HPP__

#include "lockobject.hpp"

namespace purewell
{

//! \brief Semaphore
class PWSemaphore : public PWLockObject
{
public:
	//! \brief Create semaphore.
	//! \param[in] name Semaphore name. Should lead '\'.
	//! \param[in] flags Flag.
	//! \param[in] omode Open mode.
	//! \param[in] value Initial value.
	//! \return On success, semaphore pointer.
	static PWSemaphore* create(const char* name, int flags, int omode,
		size_t value=0);

	//! \brief Create(open) semaphore.
	//! \param[in] flags Flag.
	//! \param[in] value Initial value.
	//! \return On success, semaphore pointer.
	static PWSemaphore* create(int flags, size_t value=0);

	//! \brief Release semaphore.
	//! \param[in] sem Semaphore.
	static void release(PWSemaphore* sem);

	//! \brief Unlink semaphore object from OS.
	//! \param[in] name Semaphore name.
	//! \return On success, true.
	static bool unlink(const char* name);

public:
	//! \brief Unlink semaphore object from OS.
	//! \return On success, true.
	bool unlink(void) const;
	void lock(void);
	bool lockTest(void);
	bool lockTimeLimit(size_t msec);
	void unlock(void);

	//! \brief Get value.
	//! \param[out] ret Return.
	//! \return On success, true.
	bool getValue(size_t& ret) const;

private:
	explicit PWSemaphore();
	virtual ~PWSemaphore();

};

};//namespace purewell

#endif//!__SEMAPHORE_HPP__


